package com.app.shop.activitys;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.shop.R;
import com.app.shop.adapters.TalkAdapter;
import com.app.shop.beans.GoodsBean;
import com.app.shop.beans.TalkBean;
import com.app.library.base.BaseActivity;
import com.app.library.utils.StringUtil;
import com.app.library.utils.TimeUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class  TalkActivity extends BaseActivity implements View.OnClickListener{


    private TextView tvTitle;
    private RecyclerView recy;
    private TextView tvQuest;
    private EditText edt;

    private ImageView imgvreturn;
    private TextView tvbt;
    GoodsBean goodsBean;

    TalkAdapter adapter;
    List<TalkBean> list = new ArrayList<>();
    List<TalkBean> list_all = new ArrayList<>();


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        recy= (RecyclerView) findViewById(R.id.recy);
        tvQuest= (TextView) findViewById(R.id.tv_quest);
        edt= (EditText) findViewById(R.id.edt);
        imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        tvbt= (TextView) findViewById(R.id.tv_bt);
        imgvreturn.setOnClickListener(this::onClick);
        tvbt.setOnClickListener(this::onClick);
        initData();
        tvTitle.setText("商品评论");
    }

    private void initData() {

        Bundle bundle = getIntent().getExtras();
        goodsBean = (GoodsBean) bundle.getSerializable("bean");
        list_all = DataSupport.findAll(TalkBean.class);

        tvQuest.setText("商品：" + goodsBean.getGoods_name());


        if (list_all.size() > 0) {
            for (int i = 0; i < list_all.size(); i++) {
                if (list_all.get(i).getQues_id().equals(goodsBean.goods_id)) {
                    list.add(list_all.get(i));
                }
            }
        }

        Collections.reverse(list); // 倒序排列

        adapter = new TalkAdapter(R.layout.item_list_talk, list);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

    }    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;

            case R.id.tv_bt:

                if (StringUtil.isEmpty(edt.getText().toString())) {
                    ToastUtil.showToast(this, "请输入内容");
                    return;
                }

                TalkBean bean = new TalkBean();
                bean.setCreate_time(TimeUtil.getTodayData("yyyy-MM-dd HH:mm:ss"));
                bean.setTalk_id(System.currentTimeMillis() + "");
                bean.setUser(UserManager.getUserName(this));
                bean.setUser_id(UserManager.getUserId(this));
                bean.setContent(edt.getText().toString());
                bean.setQues_id(goodsBean.goods_id);
                bean.save();

                list.add(0, bean);
                adapter.notifyDataSetChanged();


                edt.setText("");

                break;
        }
    }





}
